package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.TbFaultLog;
import com.ruoyi.system.service.ITbFaultLogService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 故障记录Controller
 * 
 * @author ruoyi
 * @date 2023-06-27
 */
@RestController
@RequestMapping("/log")
public class TbFaultLogController extends BaseController
{
    @Autowired
    private ITbFaultLogService tbFaultLogService;

    /**
     * 查询故障记录列表
     */
    @RequiresPermissions("system:log:list")
    @GetMapping("/list")
    public TableDataInfo list(TbFaultLog tbFaultLog)
    {
        startPage();
        List<TbFaultLog> list = tbFaultLogService.selectTbFaultLogList(tbFaultLog);
        return getDataTable(list);
    }

    /**
     * 导出故障记录列表
     */
    @RequiresPermissions("system:log:export")
    @Log(title = "故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbFaultLog tbFaultLog)
    {
        List<TbFaultLog> list = tbFaultLogService.selectTbFaultLogList(tbFaultLog);
        ExcelUtil<TbFaultLog> util = new ExcelUtil<TbFaultLog>(TbFaultLog.class);
        util.exportExcel(response, list, "故障记录数据");
    }

    /**
     * 获取故障记录详细信息
     */
    @RequiresPermissions("system:log:query")
    @GetMapping(value = "/{faultLogId}")
    public AjaxResult getInfo(@PathVariable("faultLogId") Long faultLogId)
    {
        return success(tbFaultLogService.selectTbFaultLogByFaultLogId(faultLogId));
    }

    /**
     * 新增故障记录
     */
    @RequiresPermissions("system:log:add")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbFaultLog tbFaultLog)
    {
        return toAjax(tbFaultLogService.insertTbFaultLog(tbFaultLog));
    }

    /**
     * 修改故障记录
     */
    @RequiresPermissions("system:log:edit")
    @Log(title = "故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbFaultLog tbFaultLog)
    {
        return toAjax(tbFaultLogService.updateTbFaultLog(tbFaultLog));
    }

    /**
     * 删除故障记录
     */
    @RequiresPermissions("system:log:remove")
    @Log(title = "故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faultLogIds}")
    public AjaxResult remove(@PathVariable Long[] faultLogIds)
    {
        return toAjax(tbFaultLogService.deleteTbFaultLogByFaultLogIds(faultLogIds));
    }
    /**
     * 添加
     */
    @RequiresPermissions("system:log:sendLog")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/{msg}")
    public AjaxResult sendLog(@PathVariable("msg") String msg)
    {
        TbFaultLog tbFault=new TbFaultLog();
        System.out.println(msg);
        String flag=msg.substring(0,msg.indexOf(","));
        String vin=msg.substring(msg.indexOf(",")+1);

        List<TbFaultLog> tbFaultLogs = tbFaultLogService.selectTbFaultLogList(tbFault);
        for (TbFaultLog tbFaultLog : tbFaultLogs) {
            if (tbFaultLog.getCarCode().equals(vin) && tbFaultLog.getFaultCode().equals(flag)){
                return null;
            }
        }

        TbFaultLog tbFaultLog=new TbFaultLog();
        tbFaultLog.setCarCode(vin);
        tbFaultLog.setFaultCode(flag);
        return toAjax(tbFaultLogService.insertTbFaultLog(tbFaultLog));
    }

//    @RequestMapping("/sendLog/{msg}")
//    public AjaxResult sendLog(@PathVariable("msg") String msg)
//    {
//        System.out.println(msg);
//        String flag=msg.substring(0,msg.indexOf(","));
//        String vin=msg.substring(msg.indexOf(",")+1);
//         TbFaultLog tbFaultLog=new TbFaultLog();
//        tbFaultLog.setCarCode(vin);
//        tbFaultLog.setFaultCode(flag);
//        return toAjax(tbFaultLogService.insertTbFaultLog(tbFaultLog));
//    }

}
